'use strict';
const { Service } = require('egg');
const bcrypt = require('bcryptjs');
const Result = require('../util/result');

class UserService extends Service {

  /**
   * 注册
   * @param {Object} data -body
   */
  async create(data) {
    let { phone, password } = data;
    const username = Math.random().toString(32).substr(-6, 6)
      .toUpperCase();
    const hash = bcrypt.hashSync(password, 10);
    password = hash;
    const pro = await this.findOne(phone);
    if (pro) return Result.warn(-1, '已存在');
    const info = await this.ctx.model.User.create({
      phone,
      password,
      username,
    });
    if (!info) return Result.fail();
    const res = {
      id: info.id,
      phone: info.phone,
      username: info.username,
    };
    return Result.success(res);
  }

  /**
   * 登录
   * @param {Object} data body
   */
  async login(data) {
    const { phone, password } = data;
    const info = await this.findOne(phone);
    if (!info) return Result.warn(-1, '帐号不存在');
    if (!bcrypt.compareSync(password, info.password)) return Result.warn(-1, '密码错误');
    const res = {
      id: info.id,
      phone: info.phone,
      username: info.username,
    };
    return Result.success(res);
  }

  async update(data) {
    const id = data.id;
    await this.ctx.model.User.update({ ...data }, {
      where: {
        id,
      },
    });
    // if (!info) {
    //   this.logger.info(info);
    //   return Result.fail();
    // }
    const info = await this.findById(id);
    const result = {
      id: info.id,
      username: info.username,
      phone: info.phone,
      email: info.email,
      city: info.city,
      gender: info.gender,
    };
    return Result.success(result);
  }

  async changePass(res) {
    const { phone, password } = res;
    const info = this.findOne(phone);
    if (!info) return Result.warn(-1, '查询失败');
    const salt = bcrypt.genSaltSync(10);
    const hash = bcrypt.hashSync(password, salt);
    const result = await this.ctx.model.User.update({ password: hash }, {
      where: {
        phone,
      },
    });
    const data = {
      id: result.id,
      username: result.username,
      phone: result.phone,
    };
    return Result.success(data);

  }

  async validatePass(res) {
    if (await this.isUser(res)) {
      return Result.noDataSuccess();
    }
    return Result.fail();
  }

  async isUser(res) {
    const { phone, password } = res;
    const info = await this.findOne(phone);
    if (!info) return Result.warn(-1, '查询失败');
    return bcrypt.compareSync(password, info.password);
  }

  async findOne(phone) {
    const info = await this.ctx.model.User.findOne({
      where: {
        phone,
      },
      attributes: ['id', 'phone', 'username', 'password'],
    });
    return info;
  }

  async findById(id) {
    const info = await this.ctx.model.User.findByPk(id);
    return info;
  }

  async findAll() {
    const info = await this.ctx.model.User.findAll({
      attributes: { exclude: ['password'] },
    });
    return info;
  }
}

module.exports = UserService;
